<?php
/**
 * Created by Silent Night.
 * Author: Silent Night
 * email: silentwolf_wp@163.com
 * Date: 2017/9/28
 * Time: 9:19
 */

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;

class Txapply extends Controller
{
    private $_model;
    private $title_field = [
    ];
    public $pagesize = 10;

    public $allMuiscTypeLists=array();
    public function _initialize()
    {
        (new \app\admin\behavior\CheckPermissions())->run();//增加权限控制
        $this->_model = new \app\admin\model\Txapply();
        $this->assign('LoginUserInfo', $this->LoginUserInfo);

        //获取当前用户的权限信息
        $this->assign('ruleLists', $this->LoginUserInfo['CurrPower']);
        $this->mapper = \app\admin\mapper\Txapply::getInstance();

    }


    /**
     * 列表页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch('index');
    }

    public function lists()
    {
        $ser_key = trim(input("request.ser_key"));//auth
        $ser_val = trim(input("request.ser_val"));//auth
        $ser_tx_type = trim(input("request.ser_tx_type"));//auth
        $ser_stats = trim(input("request.ser_stats"));//auth
        $ser_tx_source = trim(input("request.ser_tx_source"));//auth

        if ($ser_key && $ser_val) {
            $where[$ser_key] = array('like', '%' . $ser_val . '%');
        }
        if ($ser_tx_type > 0) {
            $where['tx_type'] = $ser_tx_type;
        }
        if ($ser_stats >0) {
            $where['stats'] = $ser_stats;
        }

        if($ser_tx_source==1){
            $where['source'] = 0;
        }else if($ser_tx_source==2){
            $where['source'] = 1;
        }
        $where['is_delete'] = 0;
        $lists = $this->_model->lists($where, $this->pagesize);
        $lists['data'] = changeUidToUserinfoForLists($lists['data'], 'uid','opuid');
        $lists['data'] = $this->mapper->listMapper($lists['data']);
        $this->assign('lists', $lists);
        $this->assign('request', input("request."));
        return view('lists');
    }

    /**
     * 删除
     * 支持批量删除和单条删除
     * @return mixed
     */
    public function delData()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $res = $this->_model->updateData(array('is_delete' => 1), $id);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }


    /**
     * 审核通过
     * @return mixed
     */
    public function edit_ok()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $info = $this->_model->getDataForWhere(array('id' => $id));
        if(empty($info))
        {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        if($info['is_delete'])
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经删除'];
            return json($return);
        }
        if($info['stats']==1)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现记录已经审核通过，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==2)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经标记为审核不通过，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==3)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经打款，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==4)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经打款并失败，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==5)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经退回余额，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==6)
        {
            $return = ['err' => 1, 'msg' => '操作失败,打款中'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 1,'stime'=>time()), $id);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            addTxApplyTrackLog(array(
                'ctime'=>date("Y-m-d H:i:s"),
                'pid'=>$info['id'],
                'op_uid'=>$this->LoginUserInfo['UserInfo']['id'],
                'op_username'=>$this->LoginUserInfo['UserInfo']['uname']."[".$this->LoginUserInfo['UserInfo']['nickname']."]",
                'act'=>"审核通过",
            ));
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }

    /**
     * 审核不通过
     * @return mixed
     */
    public function edit_err()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $info = $this->_model->getDataForWhere(array('id' => $id));
        if(empty($info))
        {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        if($info['is_delete'])
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经删除'];
            return json($return);
        }
        if($info['stats']==1)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现记录已经审核通过，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==2)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经标记为审核不通过，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==3)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经打款，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==4)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经打款并失败，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==5)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经退回余额，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==6)
        {
            $return = ['err' => 1, 'msg' => '操作失败,打款中'];
            return json($return);
        }
        $res = $this->_model->updateData(array('stats' => 2,'stime'=>time()), $id);
        if ($res) {
            $return = ['err' => 0, 'msg' => '操作成功'];
            addTxApplyTrackLog(array(
                'ctime'=>date("Y-m-d H:i:s"),
                'pid'=>$info['id'],
                'op_uid'=>$this->LoginUserInfo['UserInfo']['id'],
                'op_username'=>$this->LoginUserInfo['UserInfo']['uname']."[".$this->LoginUserInfo['UserInfo']['nickname']."]",
                'act'=>"审核不通过",
            ));
            return json($return);
        } else {
            $return = ['err' => 3, 'msg' => '操作失败'];
            return json($return);
        }

    }

    /**
     * 打款
     * @return mixed
     */
    public function edit_money()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $info = $this->_model->getDataForWhere(array('id' => $id));
        if(empty($info))
        {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        if($info['is_delete'])
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经删除'];
            return json($return);
        }
        if($info['stats']==0)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现记录已经未审核'];
            return json($return);
        }
        if($info['stats']==2)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经标记为审核不通过'];
            return json($return);
        }
        if($info['stats']==3)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经打款，请不要重复操作'];
            return json($return);
        }

        if($info['stats']==5)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经退回余额，请不要重复操作'];
            return json($return);
        }
        if($info['stats']==6)
        {
            $return = ['err' => 1, 'msg' => '操作失败,打款中'];
            return json($return);
        }
        if($info['tx_type']==2)//支付宝
        {
            if(empty($info['zfb_account']) || empty($info['zfb_name']))
            {
                $return = ['err' => 1, 'msg' => '操作失败,提现账号信息不完整'];
                return json($return);
            }

        }
        $this->_model->updateData(array('stats' => 6), $id);
        $return = ['err' => 0, 'msg' => '操作成功'];
        addTxApplyTrackLog(array(
            'ctime'=>date("Y-m-d H:i:s"),
            'pid'=>$info['id'],
            'op_uid'=>$this->LoginUserInfo['UserInfo']['id'],
            'op_username'=>$this->LoginUserInfo['UserInfo']['uname']."[".$this->LoginUserInfo['UserInfo']['nickname']."]",
            'act'=>"向支付宝申请打款",
        ));
        //调用第三方支付打款
        if($info['tx_type']==2)//支付宝
        {
            $payResult = alipayZhuanMoney($info['id'],$info['zfb_account'],$info['zfb_name'],$info['money']);
            if($payResult['err']==0)
            {
                $this->_model->updateData(array('stats' =>3,'ptime'=>time(),'alipay_no'=>$payResult['order_id']), $id);
                addTxApplyTrackLog(array(
                    'ctime'=>date("Y-m-d H:i:s"),
                    'pid'=>$info['id'],
                    'op_uid'=>$this->LoginUserInfo['UserInfo']['id'],
                    'op_username'=>$this->LoginUserInfo['UserInfo']['uname']."[".$this->LoginUserInfo['UserInfo']['nickname']."]",
                    'act'=>"打款成功",
                ));
            }else
            {
                $this->_model->updateData(array('stats' =>4), $id);
                addTxApplyTrackLog(array(
                    'ctime'=>date("Y-m-d H:i:s"),
                    'pid'=>$info['id'],
                    'op_uid'=>$this->LoginUserInfo['UserInfo']['id'],
                    'op_username'=>$this->LoginUserInfo['UserInfo']['uname']."[".$this->LoginUserInfo['UserInfo']['nickname']."]",
                    'act'=>"打款失败",
                ));
            }
        }

        return json($return);
    }

    public function test_money()
    {
        $res = alipayZhuanMoney(1,"13547983127",'许贵林',0.1);
        print_r($res);
    }
    /**
     * 退款到账号余额
     * @return mixed
     */
    public function edit_nomoney()
    {
        $return = [];
        $id = trim(input("request.id"));//auth
        if (empty($id)) {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        $info = $this->_model->getDataForWhere(array('id' => $id));
        if(empty($info))
        {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
        if($info['is_delete'])
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经删除'];
            return json($return);
        }
        if($info['stats']==0)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现记录未审核'];
            return json($return);
        }
        if($info['stats']==2)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经标记为审核不通过'];
            return json($return);
        }
        if($info['stats']==3)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经打款'];
            return json($return);
        }

        if($info['stats']==5)
        {
            $return = ['err' => 1, 'msg' => '操作失败,提现已经退回余额'];
            return json($return);
        }


        $result = addUserMoney($info['uid'], $info['money'], 1, "提现退款", "");
        if ($result) {
            $this->_model->updateData(array('stats' => 5), $id);
            addTxApplyTrackLog(array(
                'ctime'=>date("Y-m-d H:i:s"),
                'pid'=>$info['id'],
                'op_uid'=>$this->LoginUserInfo['UserInfo']['id'],
                'op_username'=>$this->LoginUserInfo['UserInfo']['uname']."[".$this->LoginUserInfo['UserInfo']['nickname']."]",
                'act'=>"提现退款到账户余额",
            ));
            $return = ['err' => 0, 'msg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 1, 'msg' => '操作失败'];
            return json($return);
        }
    }

    public function infoData()
    {

        $id = trim(input("request.id"));//auth

        $info = $this->_model->getDataForWhere(array('id' => $id));

        $info['UserInfo'] = array();
        if ($info['uid']) {
            $info['UserInfo'] = getUserInfoForID($info['uid']);
        }

        $info['UserInfo1'] = array();
        if ($info['opuid']) {
            $info['UserInfo1'] = getUserInfoForID($info['opuid']);
        }

        $info = $this->mapper->infoMapper($info);
//        print_r($info);

        //获取订单日志
        $orderLogModel = new \app\admin\model\TxapplyTrackLog();
        $orderLogLists = $orderLogModel ->getDataForWheres(array('pid'=>$info['id']),"*","id desc");
        $this->assign('orderLogLists', $orderLogLists);

        $this->assign('info', $info);
        $title = "详情";
        $this->assign('title', $title);
        $this->assign('id', $id);
        return view('infoData');

    }
}